// pages/demo07/image/image.js
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  //绘制原图
  drawImage01: function () {
    let ctx = this.ctx
    var img = this.canvas.createImage()
    img.crossOrigin = 'Anonymous'; //解决跨域问题
    img.onload = () => {
      console.log('成功')
      ctx.drawImage(img, 0, 0)
    }
    img.onerror = (e) => {
      console.log('失败')
    }
    img.src = '/images/demo07/weixin.jpg';
  },
  //缩放图片
  drawImage02: function () {
    let ctx = this.ctx
    var img = this.canvas.createImage()
    img.crossOrigin = 'Anonymous'; //解决跨域问题
    img.onload = () => {
      console.log('成功')
      ctx.drawImage(img, 50, 50, 200, 200)
    }
    img.onerror = (e) => {
      console.log('失败')
    }
    img.src = '/images/demo07/weixin.jpg';
  },
  //图片切割
  drawImage03: function () {
    let ctx = this.ctx
    var img = this.canvas.createImage()
    img.crossOrigin = 'Anonymous'; //解决跨域问题
    img.onload = () => {
      console.log('成功')
      ctx.drawImage(img, 210, 90, 160, 160, 50, 50, 200, 200)
    }
    img.onerror = (e) => {
      console.log('失败')
    }
    img.src = '/images/demo07/weixin.jpg';
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    const query = wx.createSelectorQuery()
    query.select("#myCanvas")
      .fields({
        node: true
      })
      .exec((res) => {
        that.canvas = res[0].node
        let canvas = that.canvas
        that.ctx = canvas.getContext('2d')

        //tip: Canvas 2D（新接口）需要显式设置画布宽高 (默认为 300x150)
        canvas.width = 300;
        canvas.height = 300;

      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})